<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\MktUser;

use common\wosotech\Util;

/**
 * MktUserSearch represents the model behind the search form about `common\models\MktUser`.
 */
class MktUserSearch extends MktUser
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'industry_id', 'area_id', 'disabled', 'status', 'subscribe', 'subscribe_time', 'sex', 'on_message_post', 'on_message_friend', 'on_message_friend_log', 'on_message_system', 'on_quiet', 'on_show_mobile', 'on_show_qq', 'on_show_weixin', 'on_show_email'], 'integer'],
            [['mobile', 'email', 'qq', 'weixin_number', 'name', 'company', 'duty', 'password', 'auth_key', 'password_hash', 'password_reset_token', 'access_token', 'create_time', 'update_time', 'openid', 'unionid', 'nickname', 'city', 'country', 'province', 'headimgurl', 'on_quiet_start_time', 'on_quiet_end_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params, $query = null)
    {
        if (null === $query) {
            $query = MktUser::find()->orderBy(['id' => SORT_DESC]);
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
        	    'pagesize' => 20,
            ],            
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'industry_id' => $this->industry_id,
            'area_id' => $this->area_id,
            'disabled' => $this->disabled,
            'status' => $this->status,
            'create_time' => $this->create_time,
            'update_time' => $this->update_time,
            'subscribe' => $this->subscribe,
            'sex' => $this->sex,
            'on_message_post' => $this->on_message_post,
            'on_message_friend' => $this->on_message_friend,
            'on_message_friend_log' => $this->on_message_friend_log,
            'on_message_system' => $this->on_message_system,
            'on_quiet' => $this->on_quiet,
            'on_quiet_start_time' => $this->on_quiet_start_time,
            'on_quiet_end_time' => $this->on_quiet_end_time,
            'on_show_mobile' => $this->on_show_mobile,
            'on_show_qq' => $this->on_show_qq,
            'on_show_weixin' => $this->on_show_weixin,
            'on_show_email' => $this->on_show_email,
        ]);

        $query->andFilterWhere(['like', 'mobile', $this->mobile])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'qq', $this->qq])
            ->andFilterWhere(['like', 'weixin_number', $this->weixin_number])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'duty', $this->duty])
            ->andFilterWhere(['like', 'openid', $this->openid])
            ->andFilterWhere(['like', 'nickname', $this->nickname])
            ->andFilterWhere(['like', 'city', $this->city])
            ->andFilterWhere(['like', 'country', $this->country])
            ->andFilterWhere(['like', 'province', $this->province]);

        return $dataProvider;
    }

    public function searchWap($params, $query = null)
    {
        if (null === $query) {
            $query = MktUser::find()
                ->andWhere (['>=', 'status', MktUser::STATUS_BASE_INFO])
                ->andWhere(['disabled' => 0])
                ->andWhere(['>', 'industry_id', 0])
                ->andWhere(['>', 'area_id', 0])                
                ->andWhere (['subscribe' => 1])
                ->orderBy(['id' => SORT_DESC]);
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'params' => $params,
                'pagesize' => 7,
                'validatePage' => false,
            ],
        ]);
        if (!($this->load($params) && $this->validate())) {
            yii::error(print_r(['no filter parameters or validate err in searchWap', $params, $query->createCommand()->getRawSql(), $this->getErrors()], true));
            return $dataProvider;
        }

        if (!empty($this->industry_id)) {
            $model = MktIndustry::findOne($this->industry_id);
            $descendantIds = $model->getDescendantsIds(null, true);
            $descendantIds[] = (int)$this->industry_id;
            //yii::error(print_r($descendantIds, true));
            $query->andFilterWhere([
                'industry_id' => $descendantIds,
            ]);
        }

        if (!empty($this->area_id)) {
            $model = MktArea::findOne($this->area_id);
            $descendantIds = $model->getDescendantsIds(null, true);
            $descendantIds[] = (int)$this->area_id;
            $query->andFilterWhere([
                'area_id' => $descendantIds,
            ]);
        }
        
        $query->andFilterWhere([
            'id' => $this->id,
            'disabled' => $this->disabled,
            'status' => $this->status,
            'subscribe' => $this->subscribe,
            'sex' => $this->sex,
        ]);

        $query->andFilterWhere(['like', 'mobile', $this->mobile])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'qq', $this->qq])
            ->andFilterWhere(['like', 'weixin_number', $this->weixin_number])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'duty', $this->duty])
            ->andFilterWhere(['like', 'nickname', $this->nickname]);

        yii::error(['has filter in searchWap', $params, $query->createCommand()->getRawSql()]);
        return $dataProvider;
    }


}
